jQuery(function($){
  // 点击添加部门显示
  $("i#js-add-department").on("click", function(){
    $("ul#depart_root").append($("li.new_department_tem"))
    $("input.new-department-name").val("");
    $("li.new_department_tem").show();
  })

  // 点击部门的保存，进行创建
  $("i.js_create_department").on("click", function(){
    var _this = this
    var department_tem = JST['settings/store_departments/item']
    var name = $("input.new-department-name").val();
    var csrf = $("input#js-settings-zuzhi-index").val();
    $.ajax({
      url: "/api/store_departments",
      method: "post",
      data: {name: name},
      headers: {
        'X-CSRF-Token': csrf
      },
      success: function(data){
        if(data.status){
          $("li.new_department_tem").hide();
          $("ul#depart_root").append(department_tem(data.department))
        }else {
          showErrorView(data.message);
        }
      }
    })
  }); //end create department

  // start to unuse department
  $(document).on("click", 'i.fa-times.js-to-remove-department', function(){
    var _this = this
    BootstrapDialog.show({
            title: '提示：',
            message: "确定移除此部门吗",
            cssClass: 'add-to-items-dialog',
            buttons: [
              {
                label: '取消',
                cssClass: '',
                action: function(dialog){
                    dialog.close();
                }
              },
              {
                  label: '确定',
                  cssClass: 'btn-primary',
                  action: function(dialog){
                      var id = $(_this).parent().data("id");
                      var csrf = $("input#js-settings-zuzhi-index").val();
            					$.ajax({
            						url: "/api/store_departments/"+ id +"/unuse",
            						method: "put",
            						headers: {
                                  'X-CSRF-Token': csrf
                                },
            						success: function(data){
            							if(data.status == true){
                            $(_this).parent().parent("li").remove();
                            dialog.close();
            							}else {
                            dialog.close();
            								showErrorView("服务器出错!");
            							}
            						}
            					})
                  }
              }
          ]
        });// end dialog
  })// end unuse department


  //点击部门显示职位相关信息
  $(document).on("click", "i.fa-list.js-to-show-positions", function(){
    var id = $(this).parent().data("id");
    $("div.branch_name").removeClass("active");
    var _this = this
    var department_show_teml = JST['settings/store_departments/show']
    $.ajax({
      url: "/api/store_departments/"+id,
      success: function(data){
        if(data.status){
          $(_this).parent().addClass("active");
          console.log(data);
          $("div.js-department-position-list").html(department_show_teml(data))
        }
      }
    })
  });

  //点击弹出新建职位的隐藏输入框
  $(document).on("click", ".fa-plus-square.js-click-to-new-position", function(){
    $("div.js-new-position-input").show();
  })

  // 点击职位隐藏框的保存
  $(document).on("click", ".fa-save.js-to-create-position", function(){
    var name = $("input.js-to-get-new-position-name").val();
    var position_tem = JST['settings/store_positions/item']
    var csrf = $("input#js-settings-zuzhi-index").val();
    var departmentId = $(this).data("department-id");
    $.ajax({
      url: "/api/store_departments/"+ departmentId +"/store_positions",
      method: "post",
      data: {name: name},
      headers: {
        'X-CSRF-Token': csrf
      },
      success: function(data){
        if(data.status){
          $("div.js-new-position-input").hide();
          $("div.js-positions-list").append(position_tem(data.store_position))
        }else {
          showErrorView(data.message);
        }
      }
    }); // end to create position
  })


  // 取消创建部门
  $("i.js-cancel-to-create-department").on("click", function(){
    $("li.new_department_tem").hide();
  })

  //点击名称修改部门
  $(document).on("click", 'span.js-change-department-span-to-input', function(){
    $(this).parent().addClass("slide_up editing")
  })

  //取消编辑部门
  $(document).on("click", "i.js-cancel-to-update-department", function(){
    $(this).parent().removeClass("slide_up editing")
  })

  //点击保存编辑部门信息
  $(document).on("click", "i.js_update_department", function(){
    var id = $(this).parent().data("id");
    var name = $(this).siblings("input").val();
    var csrf = $("input#js-settings-zuzhi-index").val();
    var _this = this
    $.ajax({
      url: "/api/store_departments/"+id,
      method: 'put',
      headers: {
        'X-CSRF-Token': csrf
      },
      data: {name: name},
      success: function(data){
        $(_this).parent().removeClass("slide_up editing");
        $(_this).siblings("span").text(name);
      }
    })
  })


  // 取消创建职位
  $(document).on("click", "i.js-cancel-to-create-position", function(){
    $("div.js-new-position-input").hide();
  });

  //点击修改更新职位
  $(document).on("click", "span.js-position-change-span-to-input", function(){
    $(this).parent().addClass("editing");
  })

  //取消更新职位信息
  $(document).on("click", "i.js-cancel-to-update-position", function(){
    $(this).parent().removeClass("editing");
  })

  //点击保存修改信息
  $(document).on("click", "i.js-to-update-position", function(){
    var _this = this
    var departmentId = $(this).data("department-id");
    var name = $(this).siblings("input").val();
    var id = $(this).parent().data("id");
    var csrf = $("input#js-settings-zuzhi-index").val();
    console.log(id);
    $.ajax({
      url: "/api/store_departments/"+ departmentId +"/store_positions/"+id,
      method: "put",
      data: {name: name},
      headers: {
        'X-CSRF-Token': csrf
      },
      success: function(data){
        if(data.status){
          $(_this).parent().removeClass("editing");
          $(_this).siblings("span").text(name);
        }
      }
    })
  })


})
